package com.luo.ai.assistant.conv.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luo.ai.assistant.conv.dao.ChatMapper;
import com.luo.ai.assistant.conv.dao.ConversationMapper;
import com.luo.ai.assistant.conv.model.ConvPageQuery;
import com.luo.ai.assistant.conv.model.Conversation;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 会话服务
 *
 * @author luohq
 * @date 2025-02-05
 */
@Service
public class ConversationService {

    private final ConversationMapper conversationMapper;
    private final ChatMapper chatMapper;

    public ConversationService(ConversationMapper conversationMapper, ChatMapper chatMapper) {
        this.conversationMapper = conversationMapper;
        this.chatMapper = chatMapper;
    }

    public Conversation addConversation() {
        Conversation conversation = new Conversation();
        // 设置会话创建时间
        conversation.setCreateTime(LocalDateTime.now());
        // 插入会话
        this.conversationMapper.insert(conversation);
        return conversation;
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteConversations(List<Long> ids) {
        // 删除会话
        this.conversationMapper.deleteBatchIds(ids);
        // 删除会话下的聊天记录
        this.chatMapper.deleteByConvIds(ids);

    }

    public Page<Conversation> getConversations(ConvPageQuery pageQuery) {
        return conversationMapper.findPage(pageQuery);
    }
}